package cn.edu.fjnu.camera;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.edu.fjnu.camera.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class StickerChoice extends Activity {
	private static final int DIALOG_YES_NO_MESSAGE = 1;
	
	private boolean Ismove=false;//判断是不是可以移动的大头贴
	private ListView myListView;
	private String[] array = 
	{
		"Star","Scenery","Cartoom","Sports"	
	};
	//ImageAdapter adapter;
	//String pathName="/sdcard/CameraSticker/Star";
	String pathName;
	ImageAdapter iamgeAdapter;
	Bitmap mSelectedBitmap;
	Intent intent;
	private HashMap<String, Object> hashMap = new HashMap<String, Object>();
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stickerchoice);
        
        
		//myListView = (ListView)findViewById(R.id.listview_choice);
        intent = this.getIntent();
        myListView = (ListView)findViewById(R.id.listview_choice1);
        final ScaleGallery g = (ScaleGallery)findViewById(R.id.gallery);
		hashMap.put("Star", "/sdcard/yang/CameraSticker/Star");
		hashMap.put("Scenery", "/sdcard/yang/CameraSticker/Scenery");
		hashMap.put("Cartoom", "/sdcard/yang/CameraSticker/Cartoom");
		hashMap.put("Sports", "/sdcard/yang/CameraSticker/Sports");
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, array);
		myListView.setAdapter(arrayAdapter);
		myListView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
			{
				pathName = (String)hashMap.get(array[arg2]);
				/////有修改
				if(array[arg2].equalsIgnoreCase("Star")){
					Ismove=true;
				}
				////
				myListView.setVisibility(View.INVISIBLE);
				iamgeAdapter = new ImageAdapter(StickerChoice.this, getImagePath(pathName));
				g.setAdapter(iamgeAdapter);
				g.setVisibility(View.VISIBLE);	
			}
		});
		
		
		//ImageAdapter iamgeAdapter = new ImageAdapter(this, getImagePath(pathName));
		
		g.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView parent, View v, int position, long id)
			{
				mSelectedBitmap = iamgeAdapter.getItem(position);
				StickerChoice.this.showDialog(DIALOG_YES_NO_MESSAGE);
			}
		});
	}
	
	protected Dialog onCreateDialog(int id)
	{
		switch(id)
		{
		case DIALOG_YES_NO_MESSAGE:
			return new AlertDialog.Builder(StickerChoice.this)
            	.setMessage(R.string.alert_dialog_title)
            	.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	///有修改
                    	if(Ismove){
                    		intent.putExtra("String","true");
                    		intent.putExtra("StickerBitmap", ImgUtil.Bitmap2Bytes(mSelectedBitmap));
                    	}
                    	/////
                    	else{
                    		///
                    		intent.putExtra("String","false");
                    		///
                    		intent.putExtra("StickerBitmap", ImgUtil.Bitmap2Bytes(mSelectedBitmap));
                    	}       
                    	setResult(StickerChoice.RESULT_OK, intent);
                    	finish();                        
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
		}
		return super.onCreateDialog(DIALOG_YES_NO_MESSAGE);
	}
	private List<String> getImagePath(String pathName)
	{
		List<String> list = new ArrayList<String>();
		//File file = new File("/sdcard/DCIM/100MEDIA");
		File file = new File(pathName);  //以路径名为参数构造File对象〄1�7
		File[] files = file.listFiles();
		for(int i=0; i < files.length; i++)
			list.add(files[i].getPath());
		return list;
	}
	
	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;
	    private List<String> list;
	    LinkedCacheMap m_linkedCacheMap;
	    List<Bitmap> m_bitmapList = new ArrayList<Bitmap>();

	    public ImageAdapter(Context c, List<String> li) {
	        mContext = c;
	        list = li;
	        TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
	        mGalleryItemBackground = a.getResourceId(
	                R.styleable.Gallery_android_galleryItemBackground, 0);
	        a.recycle();
	        m_linkedCacheMap = new LinkedCacheMap(li.size());
	        BitmapFactory.Options options = new BitmapFactory.Options();
	        options.inSampleSize = 1;
	        for(int i=0; i<li.size(); i++)
	        	m_bitmapList.add(
	        			BitmapFactory.decodeFile(li.get(i).toString(), options));
	        	
	    }

	    public int getCount() {
	        return list.size();
	    }

	    public Bitmap getItem(int position) {
	    	Bitmap bitmap = m_bitmapList.get(position);
			String tag = ""+ position;
			if(m_linkedCacheMap.has(tag)){
//				U.dout("use cache "+tag);
				return  (Bitmap)m_linkedCacheMap.get(tag);
			}else{
//				U.dout("add cache "+tag);
				m_linkedCacheMap.add(bitmap, tag);
			}
			return bitmap;
//	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        //ImageView rs = new ImageView(mContext);
	        	        
//	        i.setImageResource(mImageIds[position]);
//	        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
//	        i.setScaleType(ImageView.ScaleType.FIT_XY);
//	        i.setBackgroundResource(mGalleryItemBackground);
//	        BitmapFactory.Options options = new BitmapFactory.Options();
//	        options.inSampleSize = 1;
//	        Bitmap bitmap = BitmapFactory.decodeFile(list.get(position).toString(), options);
	        
	        
//	        i.setImageBitmap(m_bitmapList.get(position));
//	        i.setLayoutParams(new Gallery.LayoutParams(320,200));
//	        i.setScaleType(ImageView.ScaleType.FIT_XY);
//	        i.setBackgroundResource(mGalleryItemBackground);
	        
	        //我认为这里的LinearLayout是对Gallery控件的布屄1�7〄1�7
	        LinearLayout line = new LinearLayout(mContext);		
			line.setOrientation(LinearLayout.VERTICAL);
			//MirrorView rs = new MirrorView(mContext, ImgUtil.ResizeBitmap(getItem(position), 150));
			MirrorView rs = new MirrorView(mContext, ImgUtil.ResizeBitmap(getItem(position), 200));
			//rs.setImageBitmap(m_bitmapList.get(position));
			//rs.setLayoutParams(new LinearLayout.LayoutParams(150, 370));
			rs.setLayoutParams(new LinearLayout.LayoutParams(200, 370));
			line.addView(rs);
			line.setTag(position);
			return line;
	    }
	}
}

